import React, { FC } from 'react';
import { Cascader, Modal, message } from 'antd';
import { type FormPanelObject, FormPanel, InputType } from 'lfun-bg-comp';
import EmployeeGroups from './employeeGroups';
import { getCountryOptions, getProvinceCityOptions } from '@/service/options';
import { TransferOut, createTransferOut } from '@/service/teacher/transfer';
import utils from '@/utils';

const Groups: FC<{
  showTransferoutForm: boolean;
  setShowTransferoutForm: (show: boolean) => void;
  selectDataType: Array<any>;
  onFinish: () => void;
}> = ({ showTransferoutForm, setShowTransferoutForm, selectDataType, onFinish }) => {
  const transferoutList = [
    {
      title: '',
      span: 24,
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
      inputs: [
        {
          label: '调出教职工姓名',
          name: 'employee_names',
          key: 'employee_names',
          inputType: 'children',
          children: <EmployeeGroups />,
        },
        {
          label: '省市',
          name: 'province_city_id',
          key: 'province_city_id',
          required: true,
          inputType: InputType.children,
          children: <Cascader />,
          relationFields: ['district_id'],
          renderOption: async () => {
            const list = await getProvinceCityOptions(['210100']);
            return list;
          },
        },
        {
          label: '地区',
          name: 'district_id',
          key: 'district_id',
          inputType: InputType.select,
          required: true,
          renderOption: async (form: FormPanelObject) => {
            const list = await getCountryOptions(form, 'province_city_id');
            return list;
          },
        },
        {
          label: '调入单位名称',
          name: 'current_unit_name',
          key: 'current_unit_name',
          required: true,
          textRange: [0, 30],
        },
        {
          label: '调出日期',
          name: 'transfer_out_date',
          key: 'transfer_out_date',
          required: true,
          inputType: InputType.date,
        },
        {
          label: '调动原因',
          name: 'transfer_reason',
          key: 'transfer_reason',
          required: false,
          inputType: InputType.textarea,
          textRange: [0, 200],
        },
      ],
    },
  ];
  const [curForm, setCurForm] = React.useState<FormPanelObject>();
  return (
    <Modal
      open={showTransferoutForm}
      width="1000px"
      footer={null}
      title="教职工调出"
      onCancel={() => {
        setShowTransferoutForm(false);
        curForm.resetFields();
      }}
    >
      <FormPanel
        id="transferout-form"
        template={transferoutList}
        values={{ employee_names: selectDataType }}
        buttonsDisplay="block"
        submitBtnText="提交"
        restBtnText="取消"
        onLoad={async (form: FormPanelObject) => {
          setCurForm(form);
        }}
        onFinish={async (form: FormPanelObject) => {
          utils.showLoading();
          const province_id = form.getFieldValue('province_city_id')[0];
          const city_id = form.getFieldValue('province_city_id')[1];
          const district_id = form.getFieldValue('district_id');
          const teachers = form.getFieldValue('employee_names');
          const listAsync = [];
          for (let i = 0; i < teachers.length; i++) {
            const params: TransferOut = {
              current_district_province_id: province_id,
              current_district_city_id: city_id,
              current_district_area_id: district_id,
              current_region_province_id: province_id,
              current_region_city_id: city_id,
              current_region_area_id: district_id,
              teacher_id: teachers[i].teacher_id,
              current_unit_name: form.getFieldValue('current_unit_name'),
            };
            listAsync.push(createTransferOut(params));
          }
          const errorList = [];
          const res = await Promise.all(listAsync);
          for (let i = 0; i < res.length; i++) {
            const single = res[i];
            if (!single.success) {
              errorList.push(`${teachers[i].teacher_name}(${teachers[i].teacher_id_number}): 调出失败`);
            }
          }
          if (errorList.length > 0) {
            message.error(errorList.join('\n'));
          } else {
            message.success('操作成功');
            onFinish();
          }
        }}
        onReset={() => {
          setShowTransferoutForm(false);
          curForm.resetFields();
          return false;
        }}
      />
    </Modal>
  );
};

export default Groups;
